# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library")

licenses(["notice"])  # Apache v2

exports_files(["LICENSE"])

COPTS = [
    "-D__const__=__unused__",
    "-D_GNU_SOURCE",
    "-DUSE_SYMBOLIZE",
    "-DNO_TCMALLOC",
    "-D__STDC_FORMAT_MACROS",
    "-D__STDC_LIMIT_MACROS",
    "-D__STDC_CONSTANT_MACROS",
    "-DGFLAGS_NS=google",
] + select({
    "//bazel/config:krpc_with_thrift": ["-DENABLE_THRIFT_FRAMED_PROTOCOL=1"],
    "//conditions:default": [""],
}) + select({
    "//bazel/config:krpc_with_thrift_legacy_version": [],
    "//conditions:default": ["-DTHRIFT_STDCXX=std"],
}) + select({
    "//bazel/config:krpc_with_rdma": ["-DKRPC_WITH_RDMA=1"],
    "//conditions:default": [""],
})

LINKOPTS = [
    "-pthread",
    "-ldl",
] + select({
    "@bazel_tools//tools/osx:darwin": [
        "-framework CoreFoundation",
        "-framework CoreGraphics",
        "-framework CoreData",
        "-framework CoreText",
        "-framework Security",
        "-framework Foundation",
        "-Wl,-U,_MallocExtension_ReleaseFreeMemory",
        "-Wl,-U,_ProfilerStart",
        "-Wl,-U,_ProfilerStop",
        "-Wl,-U,__Z13GetStackTracePPvii",
        "-Wl,-U,_RegisterThriftProtocol",
    ],
    "//conditions:default": [
        "-lrt",
    ],
}) + select({
    "//bazel/config:krpc_with_rdma": [
        "-libverbs",
    ],
    "//conditions:default": [],
})

genrule(
    name = "config_h",
    outs = [
        "src/kutil/config.h",
    ],
    cmd = """cat << EOF  >$@""" + """
// This file is auto-generated.
#ifndef  KUTIL_CONFIG_H
#define  KUTIL_CONFIG_H
#ifdef KRPC_WITH_GLOG
#undef KRPC_WITH_GLOG
#endif
#define KRPC_WITH_GLOG """ + select({
              "//bazel/config:krpc_with_glog": "1",
              "//conditions:default": "0",
          }) +
          """
#endif  // KUTIL_CONFIG_H
EOF
    """,
)

KUTIL_SRCS = [
    "src/kutil/third_party/dmg_fp/g_fmt.cc",
    "src/kutil/third_party/dmg_fp/dtoa_wrapper.cc",
    "src/kutil/third_party/dynamic_annotations/dynamic_annotations.c",
    "src/kutil/third_party/icu/icu_utf.cc",
    "src/kutil/third_party/superfasthash/superfasthash.c",
    "src/kutil/third_party/modp_b64/modp_b64.cc",
    "src/kutil/third_party/symbolize/demangle.cc",
    "src/kutil/third_party/symbolize/symbolize.cc",
    "src/kutil/third_party/murmurhash3/murmurhash3.cpp",
    "src/kutil/arena.cpp",
    "src/kutil/at_exit.cc",
    "src/kutil/atomicops_internals_x86_gcc.cc",
    "src/kutil/base64.cc",
    "src/kutil/base64url.cc",
    "src/kutil/big_endian.cc",
    "src/kutil/cpu.cc",
    "src/kutil/debug/alias.cc",
    "src/kutil/debug/asan_invalid_access.cc",
    "src/kutil/debug/crash_logging.cc",
    "src/kutil/debug/debugger.cc",
    "src/kutil/debug/debugger_posix.cc",
    "src/kutil/debug/dump_without_crashing.cc",
    "src/kutil/debug/proc_maps_linux.cc",
    "src/kutil/debug/stack_trace.cc",
    "src/kutil/debug/stack_trace_posix.cc",
    "src/kutil/environment.cc",
    "src/kutil/files/file.cc",
    "src/kutil/files/file_posix.cc",
    "src/kutil/files/file_enumerator.cc",
    "src/kutil/files/file_enumerator_posix.cc",
    "src/kutil/files/file_path.cc",
    "src/kutil/files/file_path_constants.cc",
    "src/kutil/files/memory_mapped_file.cc",
    "src/kutil/files/memory_mapped_file_posix.cc",
    "src/kutil/files/scoped_file.cc",
    "src/kutil/files/scoped_temp_dir.cc",
    "src/kutil/file_util.cc",
    "src/kutil/file_util_posix.cc",
    "src/kutil/guid.cc",
    "src/kutil/guid_posix.cc",
    "src/kutil/hash.cc",
    "src/kutil/lazy_instance.cc",
    "src/kutil/location.cc",
    "src/kutil/memory/aligned_memory.cc",
    "src/kutil/memory/ref_counted.cc",
    "src/kutil/memory/ref_counted_memory.cc",
    "src/kutil/memory/singleton.cc",
    "src/kutil/memory/weak_ptr.cc",
    "src/kutil/posix/file_descriptor_shuffle.cc",
    "src/kutil/posix/global_descriptors.cc",
    "src/kutil/process_util.cc",
    "src/kutil/rand_util.cc",
    "src/kutil/rand_util_posix.cc",
    "src/kutil/fast_rand.cpp",
    "src/kutil/safe_strerror_posix.cc",
    "src/kutil/sha1_portable.cc",
    "src/kutil/strings/latin1_string_conversions.cc",
    "src/kutil/strings/nullable_string16.cc",
    "src/kutil/strings/safe_sprintf.cc",
    "src/kutil/strings/string16.cc",
    "src/kutil/strings/string_number_conversions.cc",
    "src/kutil/strings/string_split.cc",
    "src/kutil/strings/string_piece.cc",
    "src/kutil/strings/string_util.cc",
    "src/kutil/strings/string_util_constants.cc",
    "src/kutil/strings/stringprintf.cc",
    "src/kutil/strings/utf_offset_string_conversions.cc",
    "src/kutil/strings/utf_string_conversion_utils.cc",
    "src/kutil/strings/utf_string_conversions.cc",
    "src/kutil/synchronization/cancellation_flag.cc",
    "src/kutil/synchronization/condition_variable_posix.cc",
    "src/kutil/synchronization/waitable_event_posix.cc",
    "src/kutil/threading/non_thread_safe_impl.cc",
    "src/kutil/threading/platform_thread_posix.cc",
    "src/kutil/threading/simple_thread.cc",
    "src/kutil/threading/thread_checker_impl.cc",
    "src/kutil/threading/thread_collision_warner.cc",
    "src/kutil/threading/thread_id_name_manager.cc",
    "src/kutil/threading/thread_local_posix.cc",
    "src/kutil/threading/thread_local_storage.cc",
    "src/kutil/threading/thread_local_storage_posix.cc",
    "src/kutil/threading/thread_restrictions.cc",
    "src/kutil/threading/watchdog.cc",
    "src/kutil/time/clock.cc",
    "src/kutil/time/default_clock.cc",
    "src/kutil/time/default_tick_clock.cc",
    "src/kutil/time/tick_clock.cc",
    "src/kutil/time/time.cc",
    "src/kutil/time/time_posix.cc",
    "src/kutil/version.cc",
    "src/kutil/logging.cc",
    "src/kutil/class_name.cpp",
    "src/kutil/errno.cpp",
    "src/kutil/find_cstr.cpp",
    "src/kutil/status.cpp",
    "src/kutil/string_printf.cpp",
    "src/kutil/thread_local.cpp",
    "src/kutil/thread_key.cpp",
    "src/kutil/unix_socket.cpp",
    "src/kutil/endpoint.cpp",
    "src/kutil/fd_utility.cpp",
    "src/kutil/files/temp_file.cpp",
    "src/kutil/files/file_watcher.cpp",
    "src/kutil/time.cpp",
    "src/kutil/zero_copy_stream_as_streambuf.cpp",
    "src/kutil/crc32c.cc",
    "src/kutil/containers/case_ignored_flat_map.cpp",
    "src/kutil/iobuf.cpp",
    "src/kutil/iobuf_profiler.cpp",
    "src/kutil/binary_printer.cpp",
    "src/kutil/recordio.cc",
    "src/kutil/popen.cpp",
] + select({
    "@bazel_tools//tools/osx:darwin": [
        "src/kutil/time/time_mac.cc",
        "src/kutil/mac/scoped_mach_port.cc",
    ],
    "//conditions:default": [
        "src/kutil/file_util_linux.cc",
        "src/kutil/threading/platform_thread_linux.cc",
        "src/kutil/strings/sys_string_conversions_posix.cc",
    ],
})

objc_library(
    name = "macos_lib",
    hdrs = [
        "src/kutil/atomicops.h",
        "src/kutil/atomicops_internals_atomicword_compat.h",
        "src/kutil/atomicops_internals_mac.h",
        "src/kutil/base_export.h",
        "src/kutil/basictypes.h",
        "src/kutil/build_config.h",
        "src/kutil/compat.h",
        "src/kutil/compiler_specific.h",
        "src/kutil/containers/hash_tables.h",
        "src/kutil/debug/debugger.h",
        "src/kutil/debug/leak_annotations.h",
        "src/kutil/file_descriptor_posix.h",
        "src/kutil/file_util.h",
        "src/kutil/files/file.h",
        "src/kutil/files/file_path.h",
        "src/kutil/files/scoped_file.h",
        "src/kutil/lazy_instance.h",
        "src/kutil/logging.h",
        "src/kutil/mac/bundle_locations.h",
        "src/kutil/mac/foundation_util.h",
        "src/kutil/mac/scoped_cftyperef.h",
        "src/kutil/mac/scoped_typeref.h",
        "src/kutil/macros.h",
        "src/kutil/string_printf.h",
        "src/kutil/memory/aligned_memory.h",
        "src/kutil/memory/scoped_policy.h",
        "src/kutil/memory/scoped_ptr.h",
        "src/kutil/move.h",
        "src/kutil/port.h",
        "src/kutil/posix/eintr_wrapper.h",
        "src/kutil/scoped_generic.h",
        "src/kutil/strings/string16.h",
        "src/kutil/strings/string_piece.h",
        "src/kutil/strings/string_util.h",
        "src/kutil/strings/string_util_posix.h",
        "src/kutil/strings/sys_string_conversions.h",
        "src/kutil/synchronization/lock.h",
        "src/kutil/third_party/dynamic_annotations/dynamic_annotations.h",
        "src/kutil/third_party/murmurhash3/murmurhash3.h",
        "src/kutil/threading/platform_thread.h",
        "src/kutil/threading/thread_id_name_manager.h",
        "src/kutil/threading/thread_restrictions.h",
        "src/kutil/time.h",
        "src/kutil/time/time.h",
        "src/kutil/type_traits.h",
        ":config_h",
    ],
    enable_modules = True,
    includes = ["src/"],
    non_arc_srcs = [
        "src/kutil/mac/bundle_locations.mm",
        "src/kutil/mac/foundation_util.mm",
        "src/kutil/file_util_mac.mm",
        "src/kutil/threading/platform_thread_mac.mm",
        "src/kutil/strings/sys_string_conversions_mac.mm",
    ],
    tags = ["manual"],
    deps = [
        "@com_github_gflags_gflags//:gflags",
    ] + select({
        "//bazel/config:krpc_with_glog": ["@com_github_google_glog//:glog"],
        "//conditions:default": [],
    }),
)

cc_library(
    name = "kutil",
    srcs = KUTIL_SRCS,
    hdrs = glob([
        "src/kutil/*.h",
        "src/kutil/*.hpp",
        "src/kutil/**/*.h",
        "src/kutil/**/*.hpp",
        "src/kutil/**/**/*.h",
        "src/kutil/**/**/*.hpp",
        "src/kutil/**/**/**/*.h",
        "src/kutil/**/**/**/*.hpp",
    ]) + [
        "src/kutil/third_party/dmg_fp/dtoa.cc",
        ":config_h",
    ],
    copts = COPTS + select({
        "//bazel/config:krpc_build_for_unittest": [
            "-DKVAR_NOT_LINK_DEFAULT_VARIABLES",
            "-DUNIT_TEST",
        ],
        "//conditions:default": [],
    }),
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    "//conditions:default": ["@openssl//:ssl", "@openssl//:crypto"],
    visibility = ["//visibility:public"],
    deps = [
        "@com_github_gflags_gflags//:gflags",
        "@com_github_madler_zlib//:zlib",
        "@com_google_protobuf//:protobuf",
    ] + select({
        "//bazel/config:krpc_with_glog": ["@com_github_google_glog//:glog"],
        "//conditions:default": [],
    }) + select({
        "@bazel_tools//tools/osx:darwin": [":macos_lib"],
        "//conditions:default": [],
    })
)

cc_library(
    name = "kvar",
    srcs = glob(
        [
            "src/kvar/*.cpp",
            "src/kvar/detail/*.cpp",
        ],
        exclude = [
            "src/kvar/default_variables.cpp",
        ],
    ) + select({
        "//bazel/config:krpc_build_for_unittest": [],
        "//conditions:default": ["src/kvar/default_variables.cpp"],
    }),
    hdrs = glob([
        "src/kvar/*.h",
        "src/kvar/utils/*.h",
        "src/kvar/detail/*.h",
    ]),
    copts = COPTS + select({
        "//bazel/config:krpc_build_for_unittest": [
            "-DKVAR_NOT_LINK_DEFAULT_VARIABLES",
            "-DUNIT_TEST",
        ],
        "//conditions:default": [],
    }),
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":kutil",
    ],
)

cc_library(
    name = "kthread",
    srcs = glob([
        "src/kthread/*.cpp",
    ]),
    hdrs = glob([
        "src/kthread/*.h",
        "src/kthread/*.list",
    ]),
    copts = COPTS,
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":kutil",
        ":kvar",
    ],
)

cc_library(
    name = "json2pb",
    srcs = glob([
        "src/json2pb/*.cpp",
    ]),
    hdrs = glob([
        "src/json2pb/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":kutil",
    ],
)

cc_library(
    name = "mcpack2pb",
    srcs = [
        "src/mcpack2pb/field_type.cpp",
        "src/mcpack2pb/mcpack2pb.cpp",
        "src/mcpack2pb/parser.cpp",
        "src/mcpack2pb/serializer.cpp",
    ],
    hdrs = glob([
        "src/mcpack2pb/*.h",
    ]),
    copts = COPTS,
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":krpc_idl_options_cc_proto",
        ":kutil",
        "@com_google_protobuf//:protoc_lib",
    ],
)

filegroup(
    name = "krpc_idl_options_proto_srcs",
    srcs = [
        "src/idl_options.proto",
    ],
    visibility = ["//visibility:public"],
)

proto_library(
    name = "krpc_idl_options_proto",
    srcs = [":krpc_idl_options_proto_srcs"],
    visibility = ["//visibility:public"],
    deps = [
        "@com_google_protobuf//:descriptor_proto",
    ],
)

cc_proto_library(
    name = "krpc_idl_options_cc_proto",
    visibility = ["//visibility:public"],
    deps = [":krpc_idl_options_proto"],
)

filegroup(
    name = "krpc_internal_proto_srcs",
    srcs = glob([
        "src/krpc/*.proto",
        "src/krpc/policy/*.proto",
    ]),
    visibility = ["//visibility:public"],
)

proto_library(
    name = "krpc_internal_proto",
    srcs = [":krpc_internal_proto_srcs"],
    strip_import_prefix = "src",
    visibility = ["//visibility:public"],
    deps = [
        ":krpc_idl_options_proto",
        "@com_google_protobuf//:descriptor_proto",
    ],
)

cc_proto_library(
    name = "krpc_internal_cc_proto",
    visibility = ["//visibility:public"],
    deps = [":krpc_internal_proto"],
)

cc_library(
    name = "krpc",
    srcs = glob([
        "src/krpc/*.cpp",
        "src/krpc/**/*.cpp",
    ],
    exclude = [
        "src/krpc/thrift_service.cpp",
        "src/krpc/thrift_message.cpp",
        "src/krpc/policy/thrift_protocol.cpp",
        "src/krpc/event_dispatcher_epoll.cpp",
        "src/krpc/event_dispatcher_kqueue.cpp",
    ]) + select({
        "//bazel/config:krpc_with_thrift": glob([
            "src/krpc/thrift*.cpp",
            "src/krpc/**/thrift*.cpp",
        ]),
        "//conditions:default": [],
    }),
    hdrs = glob([
        "src/krpc/*.h",
        "src/krpc/**/*.h",
        "src/krpc/event_dispatcher_epoll.cpp",
        "src/krpc/event_dispatcher_kqueue.cpp",
    ]),
    copts = COPTS,
    includes = [
        "src/",
    ],
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":krpc_internal_cc_proto",
        ":kthread",
        ":kutil",
        ":kvar",
        ":json2pb",
        ":mcpack2pb",
        "@com_github_google_leveldb//:leveldb",
    ] + select({
        "//bazel/config:krpc_with_thrift": [
            "@org_apache_thrift//:thrift",
        ],
        "//conditions:default": [],
    }),
)

cc_binary(
    name = "protoc-gen-mcpack",
    srcs = [
        "src/mcpack2pb/generator.cpp",
    ],
    copts = COPTS,
    linkopts = LINKOPTS,
    visibility = ["//visibility:public"],
    deps = [
        ":krpc",
        ":krpc_idl_options_cc_proto",
    ],
)
